package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.ReturnResultState;
import com.sgzn.sgpay.model.request.TcpTestRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.persistence.dao.SequenceMapper;
import com.sgzn.sgpay.service.SgsTestService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.service.tcp.api.*;
import com.sgzn.sgpay.service.tcp.model.terminal.request.TcpResetTerminalRequest;
import com.sgzn.sgpay.service.tcp.model.terminal.request.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 登录接口
 * @author zhongzhongyong
 * @version Id: SgsLoginServiceImpl.java, v 0.1 2018/4/5 14:06 zhongzhongyong Exp $$
 */
@Service
@Slf4j
public class SgsTestServiceImpl implements SgsTestService {
    @Override
    public SgsResultResponse<String> invoke(TcpTestRequest request) {

        if ("2".equals(request.getTestFlag())) {
            //2.复位终端
            return tcpResetTerminalRequest();
        } else if ("3".equals(request.getTestFlag())) {
            //3.云上分
            return increaseCredit();
        } else if ("4".equals(request.getTestFlag())) {
            //4.查询终端账目(娃娃机)
            return queryTerminalRecord();
        } else if ("5".equals(request.getTestFlag())) {
            //5.查询终端参数
            return queryTerminalParam();
        } else if ("6".equals(request.getTestFlag())) {
            //6.设置终端参数(娃娃机)
            return setTerminalParamBaby();
        } else if ("7".equals(request.getTestFlag())) {
            //7.面板账目清零
            return boardBillSetZero();
        } else if ("8".equals(request.getTestFlag())) {
            //8.后台账目清零
            return backBillSetZero();
        } else if ("9".equals(request.getTestFlag())) {
            //9.查询增量（娃娃机）
            return queryIncrementBaby();
        } else if ("10".equals(request.getTestFlag())) {
            //10.恢复出厂设置
            return recoveryLeaveFactorySetup();
        } else if ("11".equals(request.getTestFlag())) {
            //11.本地账目清零
            return localBillSetZero();
        } else if ("15".equals(request.getTestFlag())) {
            //15.设置终端参数(兑币机)
            return setTerminalParamExchange();
        } else if ("16".equals(request.getTestFlag())) {
            //16.查询终端账目（兑币机）
            return queryTerminalBillExchange();
        } else if ("17".equals(request.getTestFlag())) {
            //17.查询增量（兑币机）
            return queryIncrementExchange();
        } else if ("19".equals(request.getTestFlag())) {
            //19.查询信号应答 服务
            return querySignalAnswer();
        } else if ("20".equals(request.getTestFlag())) {
            //20.定时查询总账（娃娃机）
            return clockingQueryTotalBill();
        } else if ("22".equals(request.getTestFlag())) {
            //22.清奖池
            return clearRewardPool();
        } else if ("23".equals(request.getTestFlag())) {
            //23.设置本地账目
            return setLocalBill();
        } else if ("24".equals(request.getTestFlag())) {
            //24.本轮账目清零
            return epicycleBillClear();
        } else if ("25".equals(request.getTestFlag())) {
            //25.重启系统
            return restartSystem();
        } else if ("26".equals(request.getTestFlag())) {
            //26.获取账目（供手机端查询）
            return obtainBill();
        } else if ("27".equals(request.getTestFlag())) {
            //27.设备暂停服务
            return devicePause();
        } else if ("28".equals(request.getTestFlag())) {
            //28.设备开启服务
            return deviceStart();
        }
        return SgsResponseUtil.INSTANCE.success(ReturnResultState.SUCCESS.getCode());
    }

    @Resource
    private TcpResetTerminalService tcpResetTerminalService;

    /**
     * 统一序列生成Mapper
     */
    @Resource
    private SequenceMapper sequenceMapper;

    /**
     * 2.复位终端
     * @return
     */
    private SgsResultResponse<String> tcpResetTerminalRequest() {
        TcpResetTerminalRequest tcpRequest = new TcpResetTerminalRequest();
        tcpRequest.setDeviceId("10001");

        return tcpResetTerminalService.tcpResetTerminalRequest(tcpRequest);
    }

    @Resource
    private TcpIncreaseCreditService tcpIncreaseCreditService;
    /**
     * 3.云上分
     * @return
     */
    private SgsResultResponse<String> increaseCredit() {
        TcpIncreaseCreditRequest tcpRequest = new TcpIncreaseCreditRequest();
        tcpRequest.setDeviceId("10001");
        tcpRequest.setIncrease(String.valueOf(sequenceMapper.getIncreaseCreditSequence()));
        log.info("3号指令云上分请求Log自增码: {}",tcpRequest.getIncrease());
        tcpRequest.setCoinAmount("0001");
        tcpRequest.setAmount("0001");
        return tcpIncreaseCreditService.invoke(tcpRequest);
    }

    @Resource
    private TcpQueryTerminalRecordBabyService tcpQueryTerminalRecordBabyService;
    /**
     * 4.查询终端账目(娃娃机)
     * @return
     */
    private SgsResultResponse<String> queryTerminalRecord() {
        TcpQueryTerminalRecordBabyRequest tcpRequest = new TcpQueryTerminalRecordBabyRequest();
        tcpRequest.setDeviceId("10001");
        return tcpQueryTerminalRecordBabyService.invoke(tcpRequest);
    }

    @Resource
    private TcpQueryTerminalParamService tcpQueryTerminalParamService;
    /**
     * 5.查询终端参数
     * @return
     */
    private SgsResultResponse<String> queryTerminalParam() {
        TcpQueryTerminalParamRequest tcpRequest = new TcpQueryTerminalParamRequest();
        tcpRequest.setDeviceId("10001");
        return tcpQueryTerminalParamService.invoke(tcpRequest );
    }

    @Resource
    private TcpSetTerminalParamBabyService tcpSetTerminalParamBabyService;
    /**
     * 6.设置终端参数(娃娃机)
     * @return
     */
    private SgsResultResponse<String> setTerminalParamBaby() {
        TcpSetTerminalParamBabyRequest tcpRequest = new TcpSetTerminalParamBabyRequest();
        tcpRequest.setDeviceId("10001");
        return tcpSetTerminalParamBabyService.invoke(tcpRequest);
    }


    @Resource
    private TcpBoardBillSetZeroService tcpBoardBillSetZeroService;
    /**
     * 7.面板账目清零
     * @return
     */
    private SgsResultResponse<String> boardBillSetZero() {
        TcpBoardBillSetZeroRequest tcpRequest = new TcpBoardBillSetZeroRequest();
        tcpRequest.setDeviceId("10001");
        return tcpBoardBillSetZeroService.invoke(tcpRequest);
    }

    @Resource
    private TcpBackBillSetZeroService tcpBackBillSetZeroService;
    /**
     * 8.后台账目清零
     * @return
     */
    private SgsResultResponse<String> backBillSetZero() {
        TcpBackBillSetZeroRequest tcpRequest = new TcpBackBillSetZeroRequest();
        tcpRequest.setDeviceId("10001");
        return tcpBackBillSetZeroService.invoke(tcpRequest);
    }

    @Resource
    private TcpQueryIncrementBabyService TcpQueryIncrementBabyService;
    /**
     * 9.查询增量应答（娃娃机）
     * @return
     */
    private SgsResultResponse<String> queryIncrementBaby() {
        TcpQueryIncrementBabyRequest tcpRequest = new TcpQueryIncrementBabyRequest();
        tcpRequest.setDeviceId("10001");
        return TcpQueryIncrementBabyService.invoke(tcpRequest);
    }

    @Resource
    private TcpRecoveryLeaveFactorySetupService tcpRecoveryLeaveFactorySetupService;
    /**
     * 10.恢复出厂设置
     * @return
     */
    private SgsResultResponse<String> recoveryLeaveFactorySetup() {
        TcpRecoveryLeaveFactorySetupRequest tcpRequest = new TcpRecoveryLeaveFactorySetupRequest();
        tcpRequest.setDeviceId("10001");
        return tcpRecoveryLeaveFactorySetupService.invoke(tcpRequest);
    }

    @Resource
    private TcpLocalBillSetZeroService tcpLocalBillSetZeroService;
    /**
     * 11.本地账目清零
     * @return
     */
    private SgsResultResponse<String> localBillSetZero() {
        TcpLocalBillSetZeroRequest tcpRequest = new TcpLocalBillSetZeroRequest();
        tcpRequest.setDeviceId("10001");
        return tcpLocalBillSetZeroService.invoke(tcpRequest);
    }

    @Resource
    private TcpSetTerminalParamExchangeService tcpSetTerminalParamExchangeService;
    /**
     * 15.设置终端参数(兑币机)
     * @return
     */
    private SgsResultResponse<String> setTerminalParamExchange() {
        TcpSetTerminalParamExchangeRequest tcpRequest = new TcpSetTerminalParamExchangeRequest();
        tcpRequest.setDeviceId("10001");
        tcpRequest.setData1("00");
        tcpRequest.setData2("44");
        tcpRequest.setData3("00");
        tcpRequest.setData4("33");
        tcpRequest.setData5("22");
        tcpRequest.setData6("11");
        return tcpSetTerminalParamExchangeService.invoke(tcpRequest );
    }

    @Resource
    private TcpQueryTerminalBillExchangeService tcpQueryTerminalBillExchangeService;
    /**
     * 16.查询终端账目（兑币机）
     * @return
     */
    private SgsResultResponse<String> queryTerminalBillExchange() {
        TcpQueryTerminalBillExchangeRequest tcpRequest = new TcpQueryTerminalBillExchangeRequest();
        tcpRequest.setDeviceId("10001");
        return tcpQueryTerminalBillExchangeService.invoke(tcpRequest);
    }

    @Resource
    private TcpQueryIncrementExchangeService tcpQueryIncrementExchangeService;
    /**
     * 17.查询终端账目（兑币机）
     * @return
     */
    private SgsResultResponse<String> queryIncrementExchange() {
        TcpQueryIncrementExchangeRequest tcpRequest = new TcpQueryIncrementExchangeRequest();
        tcpRequest.setDeviceId("10001");
        return tcpQueryIncrementExchangeService.invoke(tcpRequest);
    }

    /**
     * 18.预留
     */
    @Resource
    private TcpQuerySignalAnswerService tcpQuerySignalAnswerService;
    /**
     * 19.查询信号应答
     * @return
     */
    private SgsResultResponse<String> querySignalAnswer() {
        TcpQuerySignalAnswerRequest tcpRequest = new TcpQuerySignalAnswerRequest();
        tcpRequest.setDeviceId("10001");
        tcpRequest.setSignalValue("31");
        tcpRequest.setLinkServerState("0");
        return tcpQuerySignalAnswerService.invoke(tcpRequest);
    }

    @Resource
    private TcpClockingQueryTotalBillService tcpClockingQueryTotalBillService;
    /**
     * 20.定时查询总账（娃娃机）
     * @return
     */
    private SgsResultResponse<String> clockingQueryTotalBill() {
        TcpClockingQueryTotalBillRequest tcpRequest = new TcpClockingQueryTotalBillRequest();
        tcpRequest.setDeviceId("10001");
        return tcpClockingQueryTotalBillService.invoke(tcpRequest);
    }

    @Resource
    private TcpClearRewardPoolService tcpClearRewardPoolService;
    /**
     * 22.清奖池
     * @return
     */
    private SgsResultResponse<String> clearRewardPool() {
        TcpClearRewardPoolRequest tcpRequest = new TcpClearRewardPoolRequest();
        tcpRequest.setDeviceId("10001");
        return tcpClearRewardPoolService.invoke(tcpRequest);
    }

    @Resource
    private TcpSetLocalBillService tcpSetLocalBillService;
    /**
     * 23.设置本地账目
     * @return
     */
    private SgsResultResponse<String> setLocalBill() {
        TcpSetLocalBillRequest tcpRequest = new TcpSetLocalBillRequest();
        tcpRequest.setDeviceId("10001");
        tcpRequest.setData1("0");
        tcpRequest.setData2("44");
        tcpRequest.setData3("000000");
        tcpRequest.setData4("333333333");
        return tcpSetLocalBillService.invoke(tcpRequest);
    }

    @Resource
    private TcpEpicycleBillClearService tcpEpicycleBillClearService;
    /**
     * 24.本轮账目清零
     * @return
     */
    private SgsResultResponse<String> epicycleBillClear() {
        TcpEpicycleBillClearRequest tcpRequest = new TcpEpicycleBillClearRequest();
        tcpRequest.setDeviceId("10001");
        return tcpEpicycleBillClearService.invoke(tcpRequest);
    }

    @Resource
    private TcpRestartSystemService tcpRestartSystemService;
    /**
     * 25.重启系统
     * @return
     */
    private SgsResultResponse<String> restartSystem() {
        TcpRestartSystemRequest tcpRequest = new TcpRestartSystemRequest();
        tcpRequest.setDeviceId("10001");
        return tcpRestartSystemService.invoke(tcpRequest);
    }

    @Resource
    private TcpObtainBillService tcpObtainBillService;
    /**
     * 26.获取账目（供手机端查询）
     * @return
     */
    private SgsResultResponse<String> obtainBill() {
        TcpObtainBillRequest tcpRequest = new TcpObtainBillRequest();
        tcpRequest.setDeviceId("10001");
        return tcpObtainBillService.invoke(tcpRequest);
    }

    @Resource
    private TcpDevicePauseService tcpDevicePauseService;
    /**
     * 27.设备暂停服务
     * @return
     */
    private SgsResultResponse<String> devicePause() {
        TcpDevicePauseRequest tcpRequest = new TcpDevicePauseRequest();
        tcpRequest.setDeviceId("10001");
        return tcpDevicePauseService.invoke(tcpRequest);
    }

    @Resource
    private TcpDeviceStartService tcpDeviceStartService;
    /**
     * 28.设备开启服务
     * @return
     */
    private SgsResultResponse<String> deviceStart() {
        TcpDeviceStartRequest tcpRequest = new TcpDeviceStartRequest();
        tcpRequest.setDeviceId("10001");
        return tcpDeviceStartService.invoke(tcpRequest);
    }


}
